<!-- 分栏布局 -->
<template>
  <el-container class="layout xpedestal" :class="{ 'is-electron': client_type === 'electron' }">
    <div class="aside-split">
      <div class="logo flx-center">
        <!-- <img class="logo-img" src="@/assets/images/Logo.png" alt="logo" /> -->
        <Avatar type="columns" />
      </div>
      <el-scrollbar class="tw-scrollbar">
        <div class="tw-scrollbar-inner">
          <div class="split-list">
            <div
              v-for="item in menuList"
              :key="item.path"
              class="split-item"
              :class="{ 'split-active': splitActive === item.path || `/${splitActive.split('/')[1]}` === item.path }"
              @click="changeSubMenu(item)"
            >
              <SvgIcon :name="item.meta.icon" class="x56"  v-if="item.meta.icon&&item.meta.icon.indexOf('menu-')>-1" />
              <el-icon v-else>
                <component :is="item.meta.icon"></component>
              </el-icon>
              <span class="title">{{ item.meta.title }}</span>
            </div>
          </div>
          <div class="tw-scrollbar-bottom">
            <div class="bottom-list" v-if="buttomMenu.length">
              <div
                v-for="item in buttomMenu"
                :key="item.path"
                class="bottom-item"
                :class="{ 'bottom-active': splitActive === item.path || `/${splitActive.split('/')[1]}` === item.path }"
                @click="changeButtomMenu(item)"
              >
                <SvgIcon :name="item.meta.icon" class="x56"  v-if="item.meta.icon&&item.meta.icon.indexOf('menu-')>-1" />
                <el-icon v-else>
                  <component :is="item.meta.icon"></component>
                </el-icon>
              </div>
            </div>
            <div class="logo flx-center" v-else>
              <img class="logo-img" src="@/assets/images/Logo.png" alt="logo" />
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <el-aside
      :class="{ 'not-aside': !subMenuList.length, iscollapse: isCollapse }"
      :style="{ width: isCollapse ? '65px' : '210px' }"
    >
      <div class="logo flx-center">
        <span v-show="subMenuList.length" class="logo-text">{{ isCollapse ? "G" : title }}</span>
      </div>
      <el-scrollbar>
        <el-menu
          :router="false"
          :default-active="activeMenu"
          :collapse="isCollapse"
          :unique-opened="accordion"
          :collapse-transition="false"
        >
          <SubMenu :menu-list="subMenuList" />
        </el-menu>
      </el-scrollbar>
    </el-aside>
    <el-container>
      <el-header v-show="header">
        <ToolBarLeft />
        <ToolBarRight />
      </el-header>
      <Main />
    </el-container>
  </el-container>
</template>

<script setup name="layoutColumns">
import { ref, computed, watch } from "vue";
import { useRoute, useRouter } from "vue-router";

import Main from "@/layouts/components/Main/index.vue";
import ToolBarLeft from "@/layouts/components/Header/ToolBarLeft.vue";
import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
import SubMenu from "@/layouts/components/Menu/SubMenu.vue";
import Avatar from "@/layouts/components/Header/components/Avatar.vue";
import SvgIcon from '@/common/SvgIcon/index.vue'
import { useStore } from 'vuex'
const store = useStore()

// const { title, client_type } = import.meta.env;
const title = ref(import.meta.env.VITE_GLOB_APP_TITLE);
const client_type = ref(import.meta.env.VITE_CLIENT_TYPE);

const route = useRoute();
const router = useRouter();
// const authStore = useAuthStore();
// const globalStore = useGlobalStore();
const accordion = computed(() => store.state.layouts.globalState.accordion);
const isCollapse = computed(() => store.state.layouts.globalState.isCollapse);
const header = computed(() => store.state.layouts.globalState.header);
const menuList = computed(() => store.getters['layouts/showMenuListGet']) // computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.path));

const buttomMenu = ref([
  // {
  //   "path": "/forms",
  //     "name": "forms",
  //     "component": "",
  //     "meta": {
  //       "needLogin": true,
  //       "icon": "menu-cloud",
  //       "title": "云",
  //       "isLink": "",
  //       "isHide": false,
  //       "isFull": false,
  //       "isAffix": true,
  //       "isKeepAlive": true
  //     }
  // },
  // {
  //   "path": "/forms",
  //     "name": "forms",
  //     "component": "",
  //     "meta": {
  //       "needLogin": true,
  //       "icon": "menu-setting",
  //       "title": "设置",
  //       "isLink": "",
  //       "isHide": false,
  //       "isFull": false,
  //       "isAffix": true,
  //       "isKeepAlive": true
  //     }
  //   }, {
  //     "path": "/forms",
  //     "name": "forms",
  //     "component": "",
  //     "meta": {
  //       "needLogin": true,
  //       "icon": "menu-change",
  //       "title": "设置",
  //       "isLink": "",
  //       "isHide": false,
  //       "isFull": false,
  //       "isAffix": true,
  //       "isKeepAlive": true
  //     }
  // }
]);
const subMenuList = ref([]);
const splitActive = ref("");
watch(
  () => [menuList, route],
  () => {
    // 当前菜单没有数据直接 return
    if (!menuList.value.length) return;
    splitActive.value = route.path;
    const menuItem = menuList.value.filter((item) => {
      return route.path === item.path || `/${route.path.split("/")[1]}` === item.path;
    });
    if (menuItem[0]?.children?.length) return (subMenuList.value = menuItem[0]?.children);
    subMenuList.value = [];
  },
  {
    deep: true,
    immediate: true
  }
);

// change SubMenu
const changeSubMenu = (item) => {
  splitActive.value = item.path;
  if (item.children?.length) return (subMenuList.value = item.children);
  subMenuList.value = [];
  router.push(item.path);
};

const changeButtomMenu = (item) => {
  splitActive.value = item.path;
  if (item.children?.length) return (subMenuList.value = item.children);
  subMenuList.value = [];
  router.push(item.path);
};
</script>

<style scoped lang="scss">
@use "./index.scss";
</style>
